package cn.stylefeng.guns.modular.modules.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.modules.entity.ModuleInfo;
import cn.stylefeng.guns.modular.modules.mapper.ModuleInfoMapper;
import cn.stylefeng.guns.modular.modules.model.params.ModuleInfoParam;
import cn.stylefeng.guns.modular.modules.model.result.ModuleInfoResult;
import  cn.stylefeng.guns.modular.modules.service.ModuleInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 模块表 服务实现类
 * </p>
 *
 * @author dong
 * @since 2023-04-21
 */
@Service
public class ModuleInfoServiceImpl extends ServiceImpl<ModuleInfoMapper, ModuleInfo> implements ModuleInfoService {

    @Override
    public void add(ModuleInfoParam param){
        ModuleInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ModuleInfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ModuleInfoParam param){
        ModuleInfo oldEntity = getOldEntity(param);
        ModuleInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ModuleInfoResult findBySpec(ModuleInfoParam param){
        return null;
    }

    @Override
    public List<ModuleInfoResult> findListBySpec(ModuleInfoParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ModuleInfoParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ModuleInfoParam param){
        return param.getModuleId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ModuleInfo getOldEntity(ModuleInfoParam param) {
        return this.getById(getKey(param));
    }

    private ModuleInfo getEntity(ModuleInfoParam param) {
        ModuleInfo entity = new ModuleInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public List<Integer> getTasksIds(ModuleInfoParam moduleInfoParam) {
		return this.baseMapper.getTasksIds(moduleInfoParam);
	}

}
